| | | 1 | | // Copyright (c) 2020-2023 dotBunny Inc. |
| | | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | | 3 | | // See the LICENSE file in the project root for more information. |
| | | 4 | | |
| | | 5 | | using System; |
| | | 6 | | using System.Reflection; |
| | | 7 | | |
| | | 8 | | namespace GDX |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// A collection of reflection related helper utilities. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks>Torn about the existence of this utility class, yet alone the conditions dictating it.</remarks> |
| | | 14 | | public static class Reflection |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// <see cref="BindingFlags"/> for a private field. |
| | | 18 | | /// </summary> |
| | | 19 | | public const BindingFlags PrivateFieldFlags = BindingFlags.Instance | BindingFlags.NonPublic; |
| | | 20 | | /// <summary> |
| | | 21 | | /// <see cref="BindingFlags"/> for a private static. |
| | | 22 | | /// </summary> |
| | | 23 | | public const BindingFlags PrivateStaticFlags = BindingFlags.Static | BindingFlags.NonPublic; |
| | | 24 | | /// <summary> |
| | | 25 | | /// <see cref="BindingFlags"/> for a public static. |
| | | 26 | | /// </summary> |
| | | 27 | | public const BindingFlags PublicStaticFlags = BindingFlags.Static | BindingFlags.Public; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Returns the default value for a given type. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="type">A qualified type.</param> |
| | | 33 | | /// <returns>The default value.</returns> |
| | | 34 | | public static object GetDefault(this Type type) |
| | 2 | 35 | | { |
| | 2 | 36 | | if (type.IsClass || !type.IsValueType) |
| | 1 | 37 | | { |
| | 1 | 38 | | return null; |
| | | 39 | | } |
| | 1 | 40 | | return Activator.CreateInstance(type); |
| | 2 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Returns a qualified type.. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <param name="type">The full name of a type.</param> |
| | | 47 | | /// <returns>A <see cref="System.Type"/> if found.</returns> |
| | | 48 | | public static Type GetType(string type) |
| | 29 | 49 | | { |
| | 29 | 50 | | Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies(); |
| | 29 | 51 | | int loadedAssembliesCount = loadedAssemblies.Length; |
| | 5066 | 52 | | for (int i = 0; i < loadedAssembliesCount; i++) |
| | 2531 | 53 | | { |
| | 2531 | 54 | | Type targetType = loadedAssemblies[i].GetType(type); |
| | 2531 | 55 | | if (targetType != null) |
| | 27 | 56 | | { |
| | 27 | 57 | | return targetType; |
| | | 58 | | } |
| | 2504 | 59 | | } |
| | 2 | 60 | | return null; |
| | 29 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Invokes a known static method. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="type">The explicit type of the static class.</param> |
| | | 67 | | /// <param name="method">The name of the method to invoke.</param> |
| | | 68 | | /// <param name="parameters">Any parameters that should be passed to the method?</param> |
| | | 69 | | /// <param name="flags">The <paramref name="method"/>'s access flags.</param> |
| | | 70 | | /// <returns>An <see cref="object"/> of the return value. This can be null.</returns> |
| | | 71 | | public static object InvokeStaticMethod(string type, string method, object[] parameters = null, |
| | | 72 | | BindingFlags flags = PublicStaticFlags) |
| | 6 | 73 | | { |
| | 6 | 74 | | Type targetType = GetType(type); |
| | 6 | 75 | | if (targetType != null) |
| | 5 | 76 | | { |
| | 5 | 77 | | MethodInfo targetMethod = targetType.GetMethod(method, flags); |
| | 5 | 78 | | if (targetMethod != null) |
| | 4 | 79 | | { |
| | 4 | 80 | | return targetMethod.Invoke(null, parameters ?? Core.EmptyObjectArray); |
| | | 81 | | } |
| | 1 | 82 | | } |
| | 2 | 83 | | return null; |
| | 6 | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Invoke a known private method on an object. |
| | | 88 | | /// </summary> |
| | | 89 | | /// <param name="targetObject">The ambiguous object to invoke a method on.</param> |
| | | 90 | | /// <param name="method">The name of the method to invoke.</param> |
| | | 91 | | /// <param name="parameters">Any parameters that should be passed to the method?</param> |
| | | 92 | | /// <param name="flags">The <paramref name="method"/>'s access flags.</param> |
| | | 93 | | /// <returns>An <see cref="object"/> of the return value. This can be null.</returns> |
| | | 94 | | public static object InvokeMethod(object targetObject, string method, object[] parameters = null, |
| | | 95 | | BindingFlags flags = PrivateFieldFlags) |
| | 0 | 96 | | { |
| | 0 | 97 | | Type targetType = targetObject.GetType(); |
| | 0 | 98 | | MethodInfo targetMethod = targetType.GetMethod(method, flags); |
| | 0 | 99 | | return targetMethod != null ? targetMethod.Invoke(targetObject, parameters ?? Core.EmptyObjectArray) : null; |
| | 0 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Set the field or property value of a specific <paramref name="targetObject"/>, which may not be |
| | | 104 | | /// normally accessible. |
| | | 105 | | /// </summary> |
| | | 106 | | /// <param name="targetObject">The instanced object which will have it's field or property value set.</param> |
| | | 107 | | /// <param name="name">The field or property's name to set.</param> |
| | | 108 | | /// <param name="value">The value to set the field or property to.</param> |
| | | 109 | | /// <param name="fieldFlags">The field's access flags.</param> |
| | | 110 | | /// <param name="propertyFlags">The property's access flags.</param> |
| | | 111 | | /// <returns>true/false if the value was set.</returns> |
| | | 112 | | public static bool SetFieldOrPropertyValue(object targetObject, string name, object value, |
| | | 113 | | BindingFlags fieldFlags = PrivateFieldFlags, BindingFlags propertyFlags = PrivateFieldFlags) |
| | 6 | 114 | | { |
| | 6 | 115 | | if (targetObject == null) |
| | 1 | 116 | | return false; |
| | | 117 | | |
| | 5 | 118 | | Type type = targetObject.GetType(); |
| | 5 | 119 | | FieldInfo f = type.GetField(name, fieldFlags); |
| | 5 | 120 | | if (f != null) |
| | 2 | 121 | | { |
| | 2 | 122 | | f.SetValue(targetObject, value); |
| | 2 | 123 | | return true; |
| | | 124 | | } |
| | | 125 | | |
| | 3 | 126 | | PropertyInfo p = type.GetProperty(name,propertyFlags); |
| | 3 | 127 | | if (p != null) |
| | 2 | 128 | | { |
| | 2 | 129 | | p.SetValue(targetObject, value); |
| | 2 | 130 | | return true; |
| | | 131 | | } |
| | | 132 | | |
| | 1 | 133 | | return false; |
| | 6 | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Set the field value of a specific <paramref name="targetObject"/>, which may not be normally accessible. |
| | | 138 | | /// </summary> |
| | | 139 | | /// <param name="targetObject">The instanced object which will have it's field value set; use a null value if th |
| | | 140 | | /// <param name="type">The explicit type of the <paramref name="targetObject"/>.</param> |
| | | 141 | | /// <param name="name">The field's name to set.</param> |
| | | 142 | | /// <param name="value">The value to set the field to.</param> |
| | | 143 | | /// <param name="flags">The field's access flags.</param> |
| | | 144 | | /// <returns>true/false if the value was set.</returns> |
| | | 145 | | public static bool SetFieldValue(object targetObject, Type type, string name, object value, |
| | | 146 | | BindingFlags flags = PrivateFieldFlags) |
| | 6 | 147 | | { |
| | 6 | 148 | | if (type != null) |
| | 5 | 149 | | { |
| | 5 | 150 | | FieldInfo field = type.GetField(name, flags); |
| | 5 | 151 | | if (field != null) |
| | 4 | 152 | | { |
| | 4 | 153 | | field.SetValue(targetObject, value); |
| | 4 | 154 | | return true; |
| | | 155 | | } |
| | 1 | 156 | | } |
| | 2 | 157 | | return false; |
| | 6 | 158 | | } |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Set the property value of a specific <paramref name="targetObject"/>, which may not be normally accessib |
| | | 162 | | /// </summary> |
| | | 163 | | /// <param name="targetObject">The instanced object which will have it's property value set; use a null value if |
| | | 164 | | /// <param name="type">The type of the <paramref name="targetObject"/>.</param> |
| | | 165 | | /// <param name="name">The property's name to set.</param> |
| | | 166 | | /// <param name="value">The value to set the property to.</param> |
| | | 167 | | /// <param name="flags">The property's access flags.</param> |
| | | 168 | | /// <returns>true/false if the value was set.</returns> |
| | | 169 | | public static bool SetPropertyValue(object targetObject, Type type, string name, object value, |
| | | 170 | | BindingFlags flags = PrivateFieldFlags) |
| | 4 | 171 | | { |
| | 4 | 172 | | if (type != null) |
| | 3 | 173 | | { |
| | 3 | 174 | | PropertyInfo property = type.GetProperty(name, flags); |
| | 3 | 175 | | if (property != null) |
| | 2 | 176 | | { |
| | 2 | 177 | | property.SetValue(targetObject, value); |
| | 2 | 178 | | return true; |
| | | 179 | | } |
| | 1 | 180 | | } |
| | 2 | 181 | | return false; |
| | 4 | 182 | | } |
| | | 183 | | |
| | | 184 | | /// <summary> |
| | | 185 | | /// Try to access the field value of a specific <paramref name="targetObject"/>, which may not be normally a |
| | | 186 | | /// </summary> |
| | | 187 | | /// <remarks></remarks> |
| | | 188 | | /// <param name="targetObject">The instanced object which will have it's field value read; use a null value if t |
| | | 189 | | /// <param name="type">The qualified type of the <paramref name="targetObject"/>.</param> |
| | | 190 | | /// <param name="name">The field's name to read.</param> |
| | | 191 | | /// <param name="returnValue">The returned value from the field, the default value if the field was unable to be |
| | | 192 | | /// <param name="flags">The field's access flags.</param> |
| | | 193 | | /// <typeparam name="T">The type of data being read from the field.</typeparam> |
| | | 194 | | /// <returns>true/false if the process was successful.</returns> |
| | | 195 | | public static bool TryGetFieldValue<T>(object targetObject, Type type, string name, out T returnValue, BindingFl |
| | 7 | 196 | | { |
| | 7 | 197 | | if (type == null) |
| | 1 | 198 | | { |
| | 1 | 199 | | returnValue = default; |
| | 1 | 200 | | return false; |
| | | 201 | | } |
| | 6 | 202 | | FieldInfo fieldInfo = type.GetField(name, flags); |
| | 6 | 203 | | if (fieldInfo == null) |
| | 1 | 204 | | { |
| | 1 | 205 | | returnValue = default; |
| | 1 | 206 | | return false; |
| | | 207 | | } |
| | 5 | 208 | | returnValue = (T)fieldInfo.GetValue(targetObject); |
| | 5 | 209 | | return true; |
| | 7 | 210 | | } |
| | | 211 | | |
| | | 212 | | /// <summary> |
| | | 213 | | /// Try to access the field or property value of a specific <paramref name="targetObject"/>, which may not |
| | | 214 | | /// be normally accessible. |
| | | 215 | | /// </summary> |
| | | 216 | | /// <remarks>Useful for when you really do not know the <see cref="System.Type"/>.</remarks> |
| | | 217 | | /// <param name="targetObject">The instanced object which will have it's field or property value read.</param> |
| | | 218 | | /// <param name="name">The field or property's name to read.</param> |
| | | 219 | | /// <param name="returnValue">The returned value from the field or property, the default value if the property w |
| | | 220 | | /// <param name="fieldFlags">The field's access flags.</param> |
| | | 221 | | /// <param name="propertyFlags">The property's access flags.</param> |
| | | 222 | | /// <returns>true/false if a value was found.</returns> |
| | | 223 | | public static bool TryGetFieldOrPropertyValue(object targetObject, string name, out object returnValue, |
| | | 224 | | BindingFlags fieldFlags = PrivateFieldFlags, BindingFlags propertyFlags = PrivateFieldFlags) |
| | 7 | 225 | | { |
| | 7 | 226 | | if (targetObject == null) |
| | 1 | 227 | | { |
| | 1 | 228 | | returnValue = null; |
| | 1 | 229 | | return false; |
| | | 230 | | } |
| | | 231 | | |
| | 6 | 232 | | Type type = targetObject.GetType(); |
| | 6 | 233 | | FieldInfo f = type.GetField(name, fieldFlags); |
| | 6 | 234 | | if (f != null) |
| | 2 | 235 | | { |
| | 2 | 236 | | returnValue = f.GetValue(targetObject); |
| | 2 | 237 | | return true; |
| | | 238 | | } |
| | | 239 | | |
| | 4 | 240 | | PropertyInfo p = type.GetProperty(name,propertyFlags); |
| | 4 | 241 | | if (p != null) |
| | 3 | 242 | | { |
| | 3 | 243 | | returnValue = p.GetValue(targetObject); |
| | 3 | 244 | | return true; |
| | | 245 | | } |
| | | 246 | | |
| | 1 | 247 | | returnValue = default; |
| | 1 | 248 | | return false; |
| | 7 | 249 | | } |
| | | 250 | | |
| | | 251 | | /// <summary> |
| | | 252 | | /// Try to get a property value from <paramref name="targetObject"/>, which may not be normally accessible. |
| | | 253 | | /// </summary> |
| | | 254 | | /// <param name="targetObject">The instanced object which will have it's property value read; use a null value i |
| | | 255 | | /// <param name="type">The explicit type of the <paramref name="targetObject"/>.</param> |
| | | 256 | | /// <param name="name">The property's name to read.</param> |
| | | 257 | | /// <param name="returnValue">The returned value from the property, the default value if the property was unable |
| | | 258 | | /// <param name="flags">The property's access flags.</param> |
| | | 259 | | /// <typeparam name="T">The type of data being read from the property.</typeparam> |
| | | 260 | | /// <returns>true/false if the process was successful.</returns> |
| | | 261 | | public static bool TryGetPropertyValue<T>(object targetObject, Type type, string name, out T returnValue, Bindin |
| | 5 | 262 | | { |
| | 5 | 263 | | if (type == null) |
| | 1 | 264 | | { |
| | 1 | 265 | | returnValue = default; |
| | 1 | 266 | | return false; |
| | | 267 | | } |
| | 4 | 268 | | PropertyInfo propertyInfo = type.GetProperty(name, flags); |
| | 4 | 269 | | if (propertyInfo == null) |
| | 1 | 270 | | { |
| | 1 | 271 | | returnValue = default; |
| | 1 | 272 | | return false; |
| | | 273 | | } |
| | | 274 | | |
| | 3 | 275 | | returnValue = (T)propertyInfo.GetValue(targetObject); |
| | 3 | 276 | | return true; |
| | 5 | 277 | | } |
| | | 278 | | } |
| | | 279 | | } |